package com.youxin.cpcenter.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.login.UserInfo;
import com.youxin.busfoundation.bean.phone.RequestBillsBean;
import com.youxin.busfoundation.bean.phone.RequestPhoneBean;
import com.youxin.busfoundation.bean.phone.RequestPhoneCallBack;
import com.youxin.busfoundation.common.LoginAuth;
import com.youxin.busfoundation.utils.security.AesUtils;
import com.youxin.foundation.utils.ObjectUtils;
import com.youxin.foundation.utils.RegexUtils;
import com.youxin.cpcenter.common.log.LogMethod;
import com.youxin.cpcenter.service.TelphoneService;

/**
 * 电话通讯接口
 * 
 * @author yingde.cao
 *
 */
@RestController
public class TelphoneController extends BaseController {

	@Autowired
	private TelphoneService telphoneService;
	/**
	 * 电话回调接口
	 * @description 
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @param bean
	 * @return
	 */
	@LogMethod(id = "cp.telphone.callback", eventContent = {"*"})
	@PostMapping("/phone/callback")
	public ResponseObject phoneCallBack(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			RequestPhoneCallBack bean) {
		try {
			return telphoneService.saveResult(bean);
		}
		catch (DuplicateKeyException e) {
			logger.error("/phone/callback重复记录", e);
			return ResponseObject.systemError(e);
		}
		catch (Exception e) {
			logger.error("/phone/callback异常", e);
			return ResponseObject.systemError(e);
		}
	}

	@LogMethod(id = "cp.telphone.call", eventContent = {})
	@RequestMapping(value = "/api/telphone", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject telphone(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestPhoneBean bean) {
		try {
			ObjectUtils.requestNonEmpty(bean.getTo(), "电话参数不能为空");

//			if (RegexUtils.isMobile(bean.getTo()) == false && RegexUtils.isTelphone(bean.getTo()) == false) {
//				return ResponseObject.userFailure("目标电话号码格式不正确");
//			}
			return telphoneService.call(bean);
		} catch (Exception e) {
			logger.error("/api/telphone异常", e);
			return ResponseObject.systemError(e);
		}
	}

	@LogMethod(id = "cp.telphone.unbind", eventContent = {})
	@RequestMapping(value = "/api/telphone/unbind", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject unbind(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestPhoneBean bean) {
		try {
			ObjectUtils.requestNonEmpty(bean.getSubid(), "subid不能为空");
			return telphoneService.unbind(bean);
		} catch (Exception e) {
			logger.error("/api/telphone异常", e);
			return ResponseObject.systemError(e);
		}
	}
	/**
	 * 查询当前电话账单
	 * @description 
	 * @param request
	 * @param httpServletResponse
	 * @param bean
	 * @return
	 */
	@LogMethod(id = "cp.telphone.bills", eventContent = {})
	@RequestMapping(value = "/api/telphone/bills", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject bills(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestBillsBean bean) {
		try {
			ObjectUtils.requestNonEmpty(bean.getSubid(), "subid不能为空");
			return telphoneService.bills(bean);
		} catch (Exception e) {
			logger.error("/api/telphone异常", e);
			return ResponseObject.systemError(e);
		}
	}
	
	@RequestMapping(value = "/api/telphone/view", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseObject viewMobile(HttpServletRequest request, HttpServletResponse httpServletResponse,
			@RequestBody RequestPhoneBean bean) {
		try {
			ObjectUtils.requestNonNull(bean,"参数不能为空");
			ObjectUtils.requestNonEmpty(bean.getTo(), "to不能为空");
			UserInfo userInfo=LoginAuth.getUserInfo();
			String to = AesUtils.decrypt(bean.getTo(), String.valueOf(userInfo.getId()));
			return new ResponseObject(to);
		} catch (Exception e) {
			logger.error("/api/telphone/view异常", e);
			return ResponseObject.systemError(e);
		}
	}

}
